package kduck.example.query;



import com.goldgov.kduck.dao.definition.BeanDefDepository;
import com.goldgov.kduck.dao.definition.BeanEntityDef;
import com.goldgov.kduck.dao.query.QueryCreator;
import com.goldgov.kduck.dao.query.QuerySupport;
import com.goldgov.kduck.dao.sqlbuilder.ConditionBuilder;
import com.goldgov.kduck.dao.sqlbuilder.SelectBuilder;
import com.goldgov.kduck.utils.BeanDefUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

import static kduck.example.service.WorkService.*;


@Component
public class WorkQuery implements QueryCreator {

    @Override
    public QuerySupport createQuery(Map<String, Object> paramMap, BeanDefDepository depository) {
        BeanEntityDef applicantEntityDef = depository.getEntityDef(CODE_APPLICANT);
        BeanEntityDef workEntityDef = depository.getEntityDef(CODE_WORK);

        SelectBuilder selectBuilder = new SelectBuilder(paramMap);
        selectBuilder.bindFields("c", BeanDefUtils.includeField(applicantEntityDef.getFieldList(), "A_NAME"));
        selectBuilder.bindFields("s", workEntityDef.getFieldList());

        selectBuilder.from("s", workEntityDef).innerJoin("c", applicantEntityDef)
                .where()
                .and("s.NAME", ConditionBuilder.ConditionType.CONTAINS,"W_NAME");
        return selectBuilder.build();
    }
}
